package com.samsung.svmc.moneymanager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.samsung.svmc.moneymanager.adapter.TheLoaiTaiKhoanAdapter;
import com.samsung.svmc.moneymanager.adapter.TienTeAdapter;
import com.samsung.svmc.moneymanager.data.DbTaiKhoan;
import com.samsung.svmc.moneymanager.data.DbTienTe;
import com.samsung.svmc.moneymanager.object.TaiKhoan;
import com.samsung.svmc.moneymanager.object.TheLoaiTaiKhoan;
import com.samsung.svmc.moneymanager.object.TienTe;
import com.samsung.svmc.moneymanager.utility.SingletonSystem;
import com.samsung.svmc.moneymanager.utility.TheLoaiTaiKhoanUtility;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class CreateAccount extends Activity {
	
	protected String tagSQL = "SQLConnection";
	protected String tagException = "Exception";
	protected String tagDebug = "Debug";
	
	EditText txtSoTK, txtSoDu, txtHanMuc, txtGhiChu;
	Spinner comLoaiTien, comTheLoai;
	boolean bTienTe = true,bTheLoai = true;
	//String[] theLoai = new String[]{"Cá nhân","Tiết kiệm","Chia sẻ"};
	TheLoaiTaiKhoanAdapter TheLoaiAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_account);
		SingletonSystem.getInstance().setCountActivityByAdd();
		
		txtGhiChu = (EditText)findViewById(R.id.txtGhiChu);
		txtHanMuc = (EditText)findViewById(R.id.txtHanMucThang);
		txtSoDu = (EditText)findViewById(R.id.txtSoDu);
		txtSoTK = (EditText)findViewById(R.id.txtSoTaiKhoan);
		comLoaiTien = (Spinner)findViewById(R.id.comLoaiTien);
		comTheLoai = (Spinner)findViewById(R.id.comTheLoai);		
		txtSoTK.requestFocus();
		Intent intent = getIntent();
	    @SuppressWarnings("unchecked")
		HashMap<String, String> hashMap = (HashMap<String, String>)intent.getSerializableExtra("config");
	    
	    //ActionBar actionBar = getActionBar();
	    //actionBar.setDisplayUseLogoEnabled(false);
	    //actionBar.setHomeButtonEnabled(false);
	    //actionBar.setDisplayShowTitleEnabled(false);
	    //actionBar.setNavigationMode(0);
	    //actionBar.setDisplayShowCustomEnabled(true);
	    //actionBar.setCustomView(R.layout.action_create_account);
	    
	    ArrayList<TienTe> lv = DbTienTe.getInstance(this).open().getData();
	    /*
	    String[] lvTienTe = new String[lv.size()];
	    int i = 0;
	    for(TienTe tt:lv){
	    	lvTienTe[i] = tt.get_TenTienTe()+"("+tt.get_KiHieu()+")";
	    	i++;
	    }
	    */
	    TienTeAdapter TienTeAdapter = new TienTeAdapter(this, android.R.layout.simple_spinner_item,lv);
	    TienTeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
	    comLoaiTien.setAdapter(TienTeAdapter);
	    comLoaiTien.setFocusable(true);
	    comLoaiTien.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

			@Override
			public void onItemSelected(AdapterView<?> parent, View v,
					int pos, long id) {				
				// TODO Auto-generated method stub							
				if(bTienTe)
					bTienTe = false;
				else
					txtSoDu.requestFocus();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
	    	
	    });
	    /*
	    ArrayAdapter<String> TheLoaiAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, theLoai);
	    TheLoaiAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
	    */
	    TheLoaiTaiKhoanUtility tltk = new TheLoaiTaiKhoanUtility(this);
        List<TheLoaiTaiKhoan> lvTheLoai = tltk.XmlUtilyti();
	    TheLoaiAdapter = new TheLoaiTaiKhoanAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, lvTheLoai);
	    comTheLoai.setAdapter(TheLoaiAdapter);
	    comTheLoai.setFocusable(true);
	    comTheLoai.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View v,
					int pos, long id) {
				// TODO Auto-generated method stub
				if(bTheLoai)
					bTheLoai = false;
				else
					txtGhiChu.requestFocus();
				TheLoaiTaiKhoan tltk = new TheLoaiTaiKhoan();
				tltk = TheLoaiAdapter.getItem(pos);				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	    TheLoaiTaiKhoanUtility theloai = new TheLoaiTaiKhoanUtility(this);
	    
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.create_account, menu);
	    return super.onCreateOptionsMenu(menu);		
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.menu_back:
			setResult(RESULT_CANCELED);			
			finish();
			break;
		case R.id.menu_save:
			if(!checkSave())
				break;
			TaiKhoan tk = new TaiKhoan();
			tk.IconId = 1;
			tk.set_GhiChu(txtGhiChu.getText().toString());
			try{
				tk.set_HanMucThang(Double.parseDouble(txtHanMuc.getText().toString()));
			}catch (Exception e) {
				// TODO: handle exception
				tk.set_HanMucThang(0);
			}
			TienTe t = (TienTe)comLoaiTien.getSelectedItem();			
			tk.set_LoaiTienId(t.get_Id());
			TheLoaiTaiKhoan tltk = (TheLoaiTaiKhoan)comTheLoai.getSelectedItem();
			tk.set_LoaiTkId(tltk.getId());
			tk.set_NgayCapNhatCuoi(Calendar.getInstance());
			tk.set_NgayNhap(Calendar.getInstance());
			tk.set_SoDuTk(Double.parseDouble(txtSoDu.getText().toString()));
			tk.set_SoTk(txtSoTK.getText().toString());
			DbTaiKhoan db = new DbTaiKhoan(CreateAccount.this);
			db.open();
			db.insertData(tk);
			db.close();
			setResult(RESULT_OK);
			finish();
			break;
		case R.id.menu_settings:
			Toast.makeText(getBaseContext(), "settings", 1).show();
			break;

		default:
			break;
		}
		
		return true;
	}
	
	/************************* Method ********************************/
	
	public boolean checkSave(){
		return true;
	}

}
